package com.exam.controller.sys;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.exam.entity.common.ResponseResult;
import com.exam.entity.sys.SysUser;
import com.exam.service.SysUserService;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Controller
public class SysUserController {

    @Autowired
    @Qualifier("sysUserServiceImpl")
    private SysUserService sysUserService;
    public static final String CAPTCHA_ATTRIBUTE_NAME = "captcha";
    ResponseResult responseResult=new ResponseResult();


    @RequestMapping("/")
    public String login(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String userLogin(HttpServletRequest request,String u_sno,String u_psw,String captcha){
        HttpSession session = request.getSession();
        String correctCode = (String) session.getAttribute(CAPTCHA_ATTRIBUTE_NAME);
        if (!Objects.equals(captcha.toLowerCase(), correctCode.toLowerCase())){
            responseResult.setCode(0);
            responseResult.setMessage("验证码错误");
            responseResult.setResult(null);
        }
        else {
            Map<Object,Object> map = sysUserService.find_user_by_u_sno(u_sno);
            if (Integer.parseInt(String.valueOf(map.get("result_count")))==0){
                responseResult.setCode(0);
                responseResult.setMessage("用户不存在！请注册");
                responseResult.setResult(null);
            }else if(!Objects.equals(String.valueOf(map.get("u_psw")),u_psw)){
                responseResult.setCode(0);
                responseResult.setMessage("密码错误！请重试");
                responseResult.setResult(null);
            }else if(Integer.parseInt(String.valueOf(map.get("u_state")))==0){
                responseResult.setCode(0);
                responseResult.setMessage("该用户已被停用");
                responseResult.setResult(null);
            }else {
                responseResult.setCode(200);
                responseResult.setMessage("登录成功");
                responseResult.setResult(map);
                session.removeAttribute(CAPTCHA_ATTRIBUTE_NAME);
            }
        }
        return JsonUtils.getJson(responseResult);
    }

    public static boolean judge(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String inputCode = request.getParameter(CAPTCHA_ATTRIBUTE_NAME);
        String correctCode = (String) session.getAttribute(CAPTCHA_ATTRIBUTE_NAME);
        session.removeAttribute(CAPTCHA_ATTRIBUTE_NAME);
        if (inputCode == null || correctCode == null) {
            return false;
        }
        return Objects.equals(inputCode.toLowerCase(), correctCode.toLowerCase());
    }

    @RequestMapping("/stu")
    public String stu(){
        return "views/user/index";
    }

    @RequestMapping("/manage")
    public String manage(){
        return "views/manage/index";
    }
    @RequestMapping("/teacher")
    public String teacher(){
        return "views/manage/teacher_index";
    }

    @RequestMapping("/user")
    public String user(){
        return "views/manage/user_list";
    }

    @RequestMapping("/get_all_user")
    @ResponseBody
    public String get_all_user(){
        List<Map<String, Object>> all_user = sysUserService.get_all_user();
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(all_user);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_all_user1")
    @ResponseBody
    public String get_all_user1(){
        List<SysUser> all_user = sysUserService.get_all_user1();
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(all_user);
        System.out.println(JsonUtils.getJson(responseResult));
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/update_user_state_by_u_id")
    @ResponseBody
    public String update_user_state_by_u_id(int u_id,int u_state){
        Map<String, Integer> map = new HashMap<>();
        map.put("u_id",u_id);
        map.put("u_state",u_state);
        int i = sysUserService.update_user_state_by_u_id(map);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("修改成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("修改失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_user_by_u_id")
    @ResponseBody
    public String del_user_by_u_id(int u_id){
        int i = sysUserService.del_user_by_u_id(u_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/update_user_by_u_id")
    @ResponseBody
    public String update_user_by_u_id(SysUser sysUser){
        int i = sysUserService.update_user_by_u_id(sysUser);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("修改成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("修改失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/add_user")
    @ResponseBody
    public String add_user(SysUser sysUser){
        sysUser.setU_state(1);
        sysUser.setA_id(1);
        sysUser.setU_creat_time(TimeUtils.getTime());
        int i = sysUserService.add_user(sysUser);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/update_user_role_by_u_id")
    @ResponseBody
    public String update_user_role_by_u_id(SysUser sysUser){
        int i = sysUserService.update_user_role_by_u_id(sysUser);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/update_user_psw_by_u_id")
    @ResponseBody
    public String update_user_psw_by_u_id(SysUser sysUser){
        int i = sysUserService.update_user_psw_by_u_id(sysUser);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("修改成功请重新登录");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("修改失败");
        }
        return JsonUtils.getJson(responseResult);
    }



    @RequestMapping("/add_user_batch")
    @ResponseBody
    public String add_user_batch(String userList){
        JSONArray jsonArray= JSONUtil.parseArray(userList);
        List<SysUser> new_user_list = jsonArray.toList(SysUser.class);
        int index=0;
        for (SysUser user: new_user_list) {
            int user_by_u_sno = sysUserService.get_user_by_u_sno(user.getU_sno());
            if (user_by_u_sno==0){
                user.setA_id(1);
                user.setU_psw("123456");
                user.setU_creat_time(TimeUtils.getTime());
                user.setU_state(1);
                int i = sysUserService.add_user(user);
                if (i==1){
                    index+=1;
                }
            }
            else {
                System.out.println("已存在");
            }
        }
        responseResult.setCode(200);
        responseResult.setMessage("成功添加"+index+"条");
        return JsonUtils.getJson(responseResult);
    }
}
